<template xmlns="http://www.w3.org/1999/html">
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="name" placeholder="请输入物品名" suffix-icon="el-icon-search" style="width: 200px;" @keyup.enter.native="loadPost"></el-input>
      <el-select style="margin-left: 5px;" v-model="storage" placeholder="请选择仓库">
        <el-option
            v-for="item in storageData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-select style="margin-left: 5px;" v-model="goodstype" placeholder="请选择类别">
        <el-option
            v-for="item in goodsTypeData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="info" @click="add" v-if="user.roleId!==2">新增</el-button>
      <el-button type="primary" @click="inGoods" v-if="user.roleId!==2">入库</el-button>
      <el-button type="primary" @click="outGoods" v-if="user.roleId!==2">出库</el-button>
    </div>
    <el-table :data="tableData"
              :header-cell-style="{background:'#cccccc',color:'#555'}"
              border
              highlight-current-row
              @current-change="selectCurrentChange"
    >
      <el-table-column prop="id" label="ID" width="60">
      </el-table-column>
      <el-table-column prop="name" label="物品名" width="180">
      </el-table-column>
      <el-table-column prop="storage" label="仓库" width="180" :formatter="formatStorage">
      </el-table-column>
      <el-table-column prop="goodstype" label="分类" width="180" :formatter="formatGoodsType">
      </el-table-column>
      <el-table-column prop="count" label="数量" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="180" v-if="user.roleId!=2">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference" style="margin-left: 5px;" size="small" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="增加物品"
        :visible.sync="centerDialogVisible"
        width="30%"
        :before-close="handleClose"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="90px">
        <el-form-item label="物品名:" prop="name">
          <el-col :span="20">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="仓库:" prop="storage">
          <el-col :span="20">
            <el-select v-model="form.storage" placeholder="请选择仓库">
            <el-option
                v-for="item in storageData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="分类:" prop="goodstype">
          <el-col :span="20">
            <el-select v-model="form.goodstype" placeholder="请选择类别">
              <el-option
                  v-for="item in goodsTypeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-col :span="20">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
        width="62%"
        title="用户选择"
        :visible.sync="innerVisible"
        append-to-body>
      <SelectUserManage @doSelectUser="doSelectUser"></SelectUserManage>
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmUser">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
        :title="infoname"
        :visible.sync="inerDialogVisible"
        width="30%"
        :before-close="handleClose"
        center>
      <el-form ref="form1" :rules="rules1" :model="form1" label-width="90px">
        <el-form-item label="物品名:" prop="goodsname">
          <el-col :span="20">
            <el-input v-model="currentRow.name"  disabled></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="申请人:" prop="username">
          <el-col :span="20">
            <el-input v-model="form1.username" @click.native="selectUser"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="数量:" prop="count">
          <el-col :span="20">
            <el-input v-model="form1.count"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-col :span="20">
            <el-input type="textarea" v-model="form1.remark"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doInGoods">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import SelectUserManage from "@/components/user/SelectUser";
export default {
  name: "GoodsManage",
  components: {SelectUserManage},
  data() {
    let checkCount = (rule, value, callback) => {
      if(value>9999){
        callback(new Error('数量输入过大'));
      }else{
        callback();
      }
    };
    return {
      user:JSON.parse(sessionStorage.getItem("CurUser")),
      storageData:[],
      tableData: [],
      goodsTypeData:[],
      pageSize:10,
      pageNum:1,
      infoname:'',
      total:0,
      storage: '',
      goodstype: '',
      name: '',
      centerDialogVisible:false,
      inerDialogVisible:false,
      innerVisible:false,
      currentRow:{},
      tempUser:{},
      form:{
        id:'',
        name:'',
        storage:'',
        goodstype:'',
        count:0,
        remark:'',
      },
      form1:{
        goods:'',
        goodsname:'',
        count:'',
        username:'',
        userId:'',
        adminId:'',
        remark:'',
        outorin:'',
      },
      rules1:{

      },
      rules: {
        name: [
          {required: true, message: '请输入物品名', trigger: 'blur'}
        ],
        goodstype: [
          {required: true, message: '请选择分类', trigger: 'blur'}
        ],
        storage: [
          {required: true, message: '请选择仓库', trigger: 'blur'}
        ],
      },
    };
  },
  methods:{
    doSelectUser(val){
      this.tempUser =val
    },
    confirmUser(){
      this.form1.username=this.tempUser.name;
      this.form1.userId=this.tempUser.id;
      this.innerVisible = false;
    },
    selectCurrentChange(val) {
      this.currentRow = val;
    },
    formatStorage(row){
      let temp = this.storageData.find(item=>{
        return item.id===row.storage
      })
      return temp && temp.name
    },
    formatGoodsType(row){
      let temp = this.goodsTypeData.find(item=>{
        return item.id===row.goodstype
      })
      return temp && temp.name
    },
    handleSizeChange(val) {
      this.pageSize=val
      this.pageNum=1
      this.loadPost()
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    handleCurrentChange(val) {
      this.pageNum=val
      this.loadPost()
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/goods/list').then(res=>res.data).then(res=>{
      })
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/goods/listPageC1',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          storage: this.storage+'',
          goodstype: this.goodstype+'',
        }
      }).then(res=>res.data).then(res=>{
        if(res.code===200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }
      })
    },
    loadStorage(){
      this.$axios.get(this.$httpUrl+'/storage/list').then(res=>res.data).then(res=>{
        if(res.code===200){
          this.storageData=res.data
        }else {
          alert('获取数据失败')
        }
      })
    },
    loadGoodsType(){
      this.$axios.get(this.$httpUrl+'/goodstype/list').then(res=>res.data).then(res=>{
        if(res.code===200){
          this.goodsTypeData=res.data
        }else {
          alert('获取数据失败')
        }
      })
    },
    resetParam(){
      this.name=''
      this.storage=''
      this.goodstype=''
    },
    add(){
      this.centerDialogVisible=true
      this.$nextTick(()=>{
        this.resetForm();
        this.form.id=''
      })
    },
    inGoods(){
      if(!this.currentRow.id){
        alert("请选择记录")
        return
      }
      this.form1.goods=this.currentRow.id
      this.form1.adminId=this.user.id
      this.form1.goodsname=this.currentRow.name;
      this.form1.outorin='1';
      this.infoname='物品入库'
      this.$nextTick(()=>{
        this.resetInForm();
      })
      this.inerDialogVisible=true
    },
    outGoods(){
      if(!this.currentRow.id){
        alert("请选择记录")
        return
      }
      this.$nextTick(()=>{
        this.resetInForm();
      })
      this.form1.goods=this.currentRow.id
      this.form1.adminId=this.user.id
      this.form1.goodsname=this.currentRow.name
      this.form1.outorin='2';
      this.infoname='物品出库'
      this.inerDialogVisible=true
    },
    mod(row){
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.form.id = row.id
        this.form.name = row.name
        this.form.remark = row.remark
        this.form.goodstype = row.goodstype
        this.form.storage = row.storage
        this.form.count = row.count
      })

      this.loadPost()
    },
    del(id){
      this.$axios.get(this.$httpUrl+'/goods/del?id='+id,{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
        }
      }).then(res=>res.data).then(res=>{
        if(res.code===200){
          this.tableData=res.data
          this.total=res.total
          this.loadPost()
        }else {
          alert('获取数据失败')
        }
      })
    },
    doSaveOrUpdate(doSaveOrUpdate){
      this.$axios.post(this.$httpUrl+'/goods/'+doSaveOrUpdate,this.form).then(res=>res.data).then(res=>{
        if(res.code===200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible=false
        }else {
          this.$message.error('操作失败');
        }
      })

    },
    selectUser(){
      this.innerVisible=true;
    },
    doInGoods(){
      this.$axios.post(this.$httpUrl+'/record/save',this.form1).then(res=>res.data).then(res=>{
        if(res.code===200){
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.inerDialogVisible=false
          this.loadPost();
          this.resetInForm()
        }else {
          this.$message.error('操作失败');
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id){
            this.doSaveOrUpdate("update");
          }else {
            this.doSaveOrUpdate("save");
          }
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    resetInForm() {
      this.$refs.form1.resetFields();
    },
  },
  beforeMount() {
    this.loadStorage()
    this.loadGoodsType()
    this.loadPost()
  }
}
</script>

<style scoped>

</style>